package com.niubility.dao;

import com.niubility.elsaticsearch.DateBase;
import com.niubility.entity.SearchMain;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.stereotype.Repository;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("searchDao")
public class SearchDao {
    /*
    * @Author:John
    * @Description:向es中添加索引信息
    * @Date: John 2018/1/30
    * @Params: 存储的实体对象
    * @Return:成功的值
    */
    public int Insert(SearchMain searchMain) throws UnknownHostException{
        TransportClient client = DateBase.getClient();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("serName",searchMain.getName());
        map.put("context",searchMain.getMaix());
        IndexResponse response = client.prepareIndex(searchMain.getIndex(),searchMain.getType()).setSource(map).setId(String.valueOf(searchMain.getId())).get();
        //DateBase.CloseCilent(client);
        return response.getShardInfo().getSuccessful();
    }
    /*
    * @Author:John
    * @Description:删除索引
    * @Date: John 2018/1/31
    * @Params: 删除检索
    * @Return:
    */
    public int Delete(SearchMain searchMain) throws UnknownHostException{
        TransportClient client = DateBase.getClient();
        DeleteResponse response = client.prepareDelete().setIndex(searchMain.getIndex()).setType(searchMain.getType()).
                setId(String.valueOf(searchMain.getId())).execute().actionGet();
        int suf = response.getShardInfo().getSuccessful();
        //DateBase.CloseCilent(client);
        return suf;
    }
    /*
    * @Author:John
    * @Description:模糊匹配查询
    * @Date: John 2018/1/31
    * @Params:
    * @Return:
    */
    public List<Integer> Select(SearchMain searchMain) throws UnknownHostException{
        TransportClient client = DateBase.getClient();
        List<Integer> list = new ArrayList<Integer>();
//        FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("context",searchMain.getMaix()).
//                fuzziness(Fuzziness.TWO).prefixLength(2);
        MatchPhrasePrefixQueryBuilder queryBuilder = QueryBuilders.matchPhrasePrefixQuery("context",searchMain.getMaix()).slop(2);
        SearchRequestBuilder requestBuilder = client.prepareSearch(searchMain.getIndex()).setTypes(searchMain.getType());
        //按指定字段，按匹配度排序
        SearchResponse response = requestBuilder.setQuery(queryBuilder).get();
        for (SearchHit searchHit: response.getHits()){
            list.add(Integer.parseInt(searchHit.getId()));
        }
        //DateBase.CloseCilent(client);
        return list;
    }

    /*
    * @Author:John
    * @Description:更新数据
    * @Date: John 2018/1/31
    * @Params:
    * @Return:布尔值 ，修改
    */
    public boolean Update(SearchMain searchMain)throws  Exception{
        TransportClient client = DateBase.getClient();
        UpdateRequest request = new UpdateRequest();
        request.index(searchMain.getIndex());
        request.type(searchMain.getType());
        request.id(String.valueOf(searchMain.getId()));
        UpdateRequest result =  request.doc(XContentFactory.jsonBuilder().startObject()
                .field("context",searchMain.getMaix())
                .field("serName",searchMain.getName())
                .endObject()
        );
        UpdateResponse response = client.update(request).get();
        //DateBase.CloseCilent(client);
        boolean bool = false;
        if(response!=null) bool = true;
        return bool;
    }

    public static void main(String[] args) throws UnknownHostException {
        SearchDao dao = new SearchDao();
        SearchMain searchMain = new SearchMain();
        searchMain.setMaix("我在这里");
        List list = dao.Select(searchMain);
        ////System.out.println(list);
    }
}
